<template>
  <view>
    <!-- 头部导航栏 -->
    <uv-navbar title="添加机构" border left-icon="" placeholder></uv-navbar>
  </view>

  <view class="body-container">
    <!-- <uv-input type="text" placeholder="测试用"></uv-input> -->
    <uv-form :model="org" :rules="rules" ref='form' label-width="130rpx">
      <uv-form-item label="机构全称" prop="fullname" border-bottom>
        <!-- 重名问题暂未考虑 -->
        <!-- 异步校验是否重名 -->
        <uv-input v-model="org.fullname" type="text" placeholder="请输入机构全称"></uv-input>
      </uv-form-item>
      <uv-form-item label="机构类型" prop="category" border-bottom>
        <uv-input v-model="org.category" type="text" placeholder="请选择机构类型" readonly
          @click="openPicker('category')"></uv-input>
      </uv-form-item>

      <uv-form-item label="业务状态" prop="status" border-bottom>
        <uv-input v-model="org.status" type="text" placeholder="请选择业务状态" readonly
          @click="openPicker('status')"></uv-input>
      </uv-form-item>

      <uv-form-item label="其他信息" prop="otherInfo" border-bottom>
        <uv-textarea v-model="org.otherInfo" type="text" placeholder="可记录风格 需要找谁沟通 中间人等" height="500rpx"></uv-textarea>
      </uv-form-item>
    </uv-form>
  </view>

  <view>
    <!-- picker部分 -->
    <uv-picker ref="picker" :columns="pickerColumns" keyName="label" @confirm="pickerConfirm"></uv-picker>
    <!-- modal部分 -->
    <uv-modal ref="modal" :title="mTitle" :content='mContent' @confirm="modalConfirm" showCancelButton></uv-modal>
  </view>

  <view>
    <!-- 底部 -->
    <bottomTab type='submit'></bottomTab>
  </view>
</template>

<script setup>
  import {
    onLoad
  } from '@dcloudio/uni-app'
  import {
    ref
  } from 'vue';
  import nx from '@/nx';
  import organization from '../../nx/api/organization';

  defineOptions({
    name: "addOrganization",
  })


  const orgApi = nx.$api.organization
  const orgCategory = ref()

  const picker = ref()
  const pickerColumns = ref()

  const org = ref({
    fullname: '',
    category: '', // 公司类型  城投平台，产投平台，审计机构，律师事务所，金租公司，商租公司，信托公司，证券公司，银行自营，银行理财，券商理财
    cat_id: '',
    status: '', // 联系状态： 业务合作中，机会跟踪，潜在客户，开始接触
    sid: '',
    otherInfo: ''
  })

  const openPicker = async (e) => {
    if (e === 'category') {
      let resp = await orgApi.getOrgCat()
      if (resp.code === 200) {
        // console.log("类型",resp.data);
        let pickerList = resp.data.map(item => {
          return {
            label: item.category,
            id: item.cat_id,
            type: 'category'
          }
        })
        // console.log(pickerList);
        pickerColumns.value = [pickerList]
      }
      // pickerColumns.value = 
    }
    if (e === 'status') {
      let resp = await orgApi.getOrgStatus()
      if (resp.code === 200) {
        // console.log("类型",resp.data);
        let pickerList = resp.data.map(item => {
          return {
            label: item.status,
            id: item.sid,
            type: 'status'
          }
        })
        // console.log(pickerList);
        pickerColumns.value = [pickerList]
      }
      // pickerColumns.value = 
    }
    picker.value.open()
  }

  const pickerConfirm = (e) => {
    let val = e.value[0]
    if (val.type === 'category') {
      org.value.category = val.label
      org.value.cat_id = val.id
      console.log('添加category后', org.value);
    }
    if (val.type === 'status') {
      org.value.status = val.label
      org.value.sid = val.id
      console.log('添加status后', org.value);
    }
  }

  const submit = async () => {
    let resp = await orgApi.addOrganization(org.value)
    if (resp.code === 200) {
      console.log(resp.data);
      uni.navigateBack(1)
    }
  }

  onLoad(() => {
    uni.$on('bottomTabSubmit', () => {
      submit()
    })

  })
</script>

<style lang="scss">

</style>